﻿#include "syssubprotocoleditor.h"
#include "ui_syssubprotocoleditor.h"
#include "systemparamdef.h"

SysSubProtocolEditor::SysSubProtocolEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SysSubProtocolEditor)
{
    ui->setupUi(this);
    ui->TypeBox->setView(new QListView());
    nOldSubId = -1;
    nEditType =ADD_DATA;
}

SysSubProtocolEditor::~SysSubProtocolEditor()
{
    delete ui;
}

void SysSubProtocolEditor::InitSysSubProtocolEditor(int nType,int nId ,int nMsgType ,const QString sMsg)
{
    ui->IDInput->clear();
    ui->NameInput->clear();
    QString sTitleName = "";
    QString sIconPath = "";
    nOldSubId = nId;
    nEditType =nType;
    switch (nType)
    {
    case ADD_DATA:
    {
        sTitleName = "添加协议";
        sIconPath =":/Icon/Add.png";
        break;
    }
    case EDIT_DATA:
    {
        sTitleName = "编辑协议";
        sIconPath =":/Icon/Edit.png";
        ui->IDInput->setText(QString::number(nId));
        ui->NameInput->setText(sMsg);
        ui->TypeBox->setCurrentIndex(nMsgType);
        break;
    }
    }
    setWindowIcon(QIcon(sIconPath));
    setWindowTitle(sTitleName);
    setFixedSize(400,300);
    ui->IDInput->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
    ui->TypeBox->setStyleSheet("QComboBox QAbstractItemView::item{height:25px;}"  );//下拉选项高度
}

void SysSubProtocolEditor::on_QuitBtn_clicked()
{
    ui->IDInput->clear();
    ui->NameInput->clear();
    close();
}

void SysSubProtocolEditor::on_EnterBtn_clicked()
{
    int nSubId =ui->IDInput->text().toInt();
    QString sData =  ui->NameInput->text();
    int nType = ui->TypeBox->currentIndex();

    if(nSubId <0 )
    {
        QMessageBox::critical(this, "错误", "Id不能小于0!","确定");
        return;
    }

    if(sData.isEmpty())
    {
        QMessageBox::critical(this, "错误", "数据描述不能为空","确定");
        return;
    }
    close();
    if(nEditType == ADD_DATA)
    {
        emit on_CreateSysSubProtocol(nSubId,sData,nType);
    }else
    {
        emit on_EditorSysSubProtocol(nSubId,sData,nType,nOldSubId);
    }
}
